home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cpost_1_4.lha / cpostutl.c < prev    next >
C/C++ Source or Header  |  1997-11-17  |  38KB  |  1,310 lines

  1. /*------------------------------------------------------------------
  2.  * cpostutl.c : utilities for cPost
  3.  *------------------------------------------------------------------
  4.  * 11-23-91 originally by Patrick J. Mueller
  5.  * 12-03-92 converted from cBook to cPost
  6.  *------------------------------------------------------------------*/
  7.  
  8. #ifdef AMIGA
  9. #define USE_BUILTIN_MATH
  10. #endif /* AMIGA */
  11.  
  12. #if defined(OPSYS_OS2) || defined(OPSYS_OS2V2)
  13.    #define INCL_BASE
  14.    #include <os2.h>
  15. #endif
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <stdarg.h>
  22. #include <time.h>
  23.  
  24. #include "ctok.h"
  25. #include "cpost.h"
  26. #include "tokfile.h"
  27.  
  28. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  29. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  30.  
  31. /*------------------------------------------------------------------
  32.  * compare two file names
  33.  *------------------------------------------------------------------*/
  34. int FileNameCompare(
  35.    File *file1,
  36.    File *file2
  37.    )
  38.    {
  39.    int   scmp;
  40.    int   i;
  41.    char *name1;
  42.    char *name2;
  43.  
  44.    /*---------------------------------------------------------------
  45.     * get the base file names
  46.     *---------------------------------------------------------------*/
  47.    name1 = malloc(1+strlen(file1->name));
  48.    name2 = malloc(1+strlen(file2->name));
  49.  
  50.    if (!name1 || !name2)
  51.       cPostError(1,"out of memory!!!");
  52.  
  53.    strcpy(name1,file1->name);
  54.    strcpy(name2,file2->name);
  55.  
  56.    if (strchr(name1,'.')) *strchr(name1,'.') = 0;
  57.    if (strchr(name2,'.')) *strchr(name2,'.') = 0;
  58.  
  59.    /*---------------------------------------------------------------
  60.     * sort each category
  61.     *---------------------------------------------------------------*/
  62.    for (i=0; i<(int)strlen(info.oSort); i++)
  63.       {
  64.       /*------------------------------------------------------------
  65.        * sort by type
  66.        *------------------------------------------------------------*/
  67.       if ('T' == info.oSort[i])
  68.          {
  69.          if (file1->type < file2->type)
  70.             {
  71.             free(name1);
  72.             free(name2);
  73.             return -1;
  74.             }
  75.          if (file1->type > file2->type)
  76.             {
  77.             free(name1);
  78.             free(name2);
  79.             return  1;
  80.             }
  81.  
  82.          scmp = Stricmp(file1->ext,file2->ext);
  83.          if (scmp)
  84.             {
  85.             free(name1);
  86.             free(name2);
  87.             return  scmp;
  88.             }
  89.          }
  90.  
  91.       /*------------------------------------------------------------
  92.        * sort by name
  93.        *------------------------------------------------------------*/
  94.       else if ('N' == info.oSort[i])
  95.          {
  96.          /*---------------------------------------------------------
  97.           * sort by name
  98.           *---------------------------------------------------------*/
  99.          scmp = Stricmp(file1->name,file2->name);
  100.          if (scmp)
  101.             {
  102.             free(name1);
  103.             free(name2);
  104.             return  scmp;
  105.             }
  106.          }
  107.       }
  108.  
  109.    free(name1);
  110.    free(name2);
  111.  
  112.    return 0;
  113.    }
  114.  
  115. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  116. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  117.  
  118. /*------------------------------------------------------------------
  119.  * compare two strings indirectly
  120.  *------------------------------------------------------------------*/
  121. int IdentCompare(
  122.    char **str1,
  123.    char **str2
  124.    )
  125.    {
  126.    return strcmp(*str1,*str2);
  127.    }
  128.  
  129. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  130. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  131.  
  132. /*------------------------------------------------------------------
  133.  * compare two function names
  134.  *------------------------------------------------------------------*/
  135. int FunctionNameCompare(
  136.    Function *func1,
  137.    Function *func2
  138.    )
  139.    {
  140.    int cmp;
  141.  
  142.    cmp = Stricmp(func1->name,func2->name);
  143.    if (cmp) return cmp;
  144.  
  145.    cmp = strcmp(func1->name,func2->name);
  146.    if (cmp) return cmp;
  147.  
  148.    return 0;
  149.    }
  150.  
  151. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  152. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  153.  
  154. /*------------------------------------------------------------------
  155.  * compare two function names (via indirection)
  156.  *------------------------------------------------------------------*/
  157. int FunctionNamePtrCompare(
  158.    Function **func1,
  159.    Function **func2
  160.    )
  161.    {
  162.    return FunctionNameCompare(*func1,*func2);
  163.    }
  164.  
  165. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  166. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  167.  
  168. /*------------------------------------------------------------------
  169.  * hash an identifier
  170.  *------------------------------------------------------------------*/
  171. int IdentHash(
  172.    char     **str,
  173.    int        hashSize
  174.    )
  175.    {
  176.    int   hash;
  177.    char *c;
  178.  
  179.    for (hash=0, c=*str; *c; c++)
  180.       hash +=*c;
  181.  
  182.    return hash % hashSize;
  183.    }
  184.  
  185. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  186. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  187.  
  188. /*------------------------------------------------------------------
  189.  * add a file to the list of files
  190.  *------------------------------------------------------------------*/
  191. void FileAdd(
  192.    Info      *info,
  193.    List      *fileList,
  194.    char      *name,
  195.    char      *pathName,
  196.    char      *fDate,
  197.    char      *fTime
  198.    )
  199.    {
  200.    File  file;
  201.    char *type;
  202.    char *test;
  203.    char *ext;
  204.  
  205.    /*---------------------------------------------------------------
  206.     * copy the name in
  207.     *---------------------------------------------------------------*/
  208.    file.name = malloc(1+strlen(name));
  209.    if (NULL == file.name)
  210.       cPostError(1,"out of memory!!!");
  211.  
  212.    strcpy(file.name,name);
  213.  
  214.    file.pathName = malloc(1+strlen(name)+strlen(pathName));
  215.    if (NULL == file.pathName)
  216.       cPostError(1,"out of memory!!!");
  217.  
  218.    strcpy(file.pathName,pathName);
  219.    strcat(file.pathName,name);
  220.  
  221.    strcpy(file.date,fDate);
  222.    strcpy(file.time,fTime);
  223.  
  224.    /*---------------------------------------------------------------
  225.     * get the extension
  226.     *---------------------------------------------------------------*/
  227.    ext = strchr(file.name,'.');
  228.    if (!ext)
  229.       file.ext = "";
  230.    else
  231.       {
  232.       ext++;
  233.       file.ext = malloc(1+strlen(ext));
  234.       if (!file.ext)
  235.          cPostError(1,"out of memory!!!");
  236.  
  237.       strcpy(file.ext,ext);
  238.  
  239.       /*------------------------------------------------------------
  240.        * remove . and anything following it
  241.        *------------------------------------------------------------*/
  242.       if (strchr(file.ext,'.'))
  243.          *strchr(file.ext,'.') = '\0';
  244.       }
  245.  
  246.    /*---------------------------------------------------------------
  247.     * default the type to 'other'
  248.     *---------------------------------------------------------------*/
  249.    file.type = 2;
  250.  
  251.    /*---------------------------------------------------------------
  252.     * see if it's an 'H' file
  253.     *---------------------------------------------------------------*/
  254.    type = malloc(1+strlen(info->oHtype));
  255.    if (!type)
  256.       cPostError(1,"out of memory!!!");
  257.  
  258.    strcpy(type,info->oHtype);
  259.    test = strtok(type,", ");
  260.    while (test)
  261.       {
  262.       if (!Stricmp(file.ext,test))
  263.          file.type = 0;
  264.  
  265.       test = strtok(NULL,", ");
  266.       }
  267.  
  268.    free(type);
  269.  
  270.    /*---------------------------------------------------------------
  271.     * see if it's a 'C' file
  272.     *---------------------------------------------------------------*/
  273.    type = malloc(1+strlen(info->oCtype));
  274.    if (!type)
  275.       cPostError(1,"out of memory!!!");
  276.  
  277.    strcpy(type,info->oCtype);
  278.    test = strtok(type,", ");
  279.    while (test)
  280.       {
  281.       if (!Stricmp(file.ext,test))
  282.          file.type = 1;
  283.  
  284.       test = strtok(NULL,", ");
  285.       }
  286.  
  287.    free(type);
  288.  
  289.    /*---------------------------------------------------------------
  290.     * set rest of stuff
  291.     *---------------------------------------------------------------*/
  292.    file.line      = NULL;
  293.    file.lines     = 0L;
  294.    file.cline     = 0L;
  295.    file.tempName  = NULL;
  296.    file.breakList = NULL;
  297.  
  298.    file.funcDefList = ListCreate(siz